<template>
	<view class="content">
		尊贵的会员
	</view>
</template>

<script>
	import cropper from "../../components/lxiaoxiao-cropper/lxiaoxiao-cropper.vue";
	export default {
		components: {
			cropper
		},
		data() {
			return {
				fileList1: [],
			}
		},
		mounted() {
		},
		onLoad() {

		},
		methods: {
			//上传返回图片
			myUpload(rsp) {
				const self = this;
				// self.imgurl = rsp.path; //更新头像方式一
				this.splitImageIntoBase64(
					rsp.path, (
						base64Images) => {
						this.imgsrc = base64Images
					});
			},
			saveBase64Image(base64, fileName) {

				// 创建一个a元素用于触发下载
				const link = document.createElement('a');
				link.style.display = 'none'; // 隐藏链接
				link.href = base64; // 将Base64字符串设置为下载链接
				link.download = fileName; // 设置下载文件名

				// 将a元素添加到文档中，并模拟点击
				document.body.appendChild(link);
				link.click();

				// 移除a元素
				document.body.removeChild(link);
			},
			splitImageIntoBase64(imageUrl, callback) {
				const img = new Image();
				img.crossOrigin = 'Anonymous'; // 如果图片需要跨域访问，可能需要设置这个属性
				img.onload = function() {
					const canvas = document.createElement('canvas');
					const ctx = canvas.getContext('2d');
					const tileSize = Math.ceil(img.width / 3.01); // 假设切割成3x3九宫格

					const base64Images = [];
					for (let row = 0; row < 3; row++) {
						for (let col = 0; col < 3; col++) {
							canvas.width = tileSize;
							canvas.height = tileSize;
							ctx.drawImage(img, col * tileSize, row * tileSize, tileSize, tileSize, 0, 0, tileSize,
								tileSize);
							const base64Image = canvas.toDataURL('image/jpeg'); // 或者 'image/png' 如果你需要PNG格式
							base64Images.push(base64Image);
						}
					}

					callback(base64Images); // 使用回调函数返回所有的Base64字符串
				};
				img.src = imageUrl;
			},

			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1);
			},
			// 新增图片
			async afterRead(event) {
				debugger
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file);
				let fileListLen = this[`fileList${event.name}`].length;
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: "uploading",
						message: "上传中",
					});
				});
				this.splitImageIntoBase64(
					this.fileList1[0].url, (
						base64Images) => {
						this.imgsrc = base64Images
					});
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url);
					let item = this[`fileList${event.name}`][fileListLen];
					this[`fileList${event.name}`].splice(
						fileListLen,
						1,
						Object.assign(item, {
							status: "success",
							message: "",
							url: result,
						})
					);
					fileListLen++;
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: "http://192.168.2.21:7001/upload", // 仅为示例，非真实的接口地址
						filePath: url,
						name: "file",
						formData: {
							user: "test",
						},
						success: (res) => {
							setTimeout(() => {
								resolve(res.data.data);
							}, 1000);
						},
					});
				});
			},
			
			onSaveAll(){
				this.imgsrc.forEach((item,index)=>{
					const link = document.createElement('a');
					link.style.display = 'none'; // 隐藏链接
					link.href = item; // 将Base64字符串设置为下载链接
					link.download = `image${index}.png`; // 设置下载文件名
					
					// 将a元素添加到文档中，并模拟点击
					document.body.appendChild(link);
					link.click();
					
					// 移除a元素
					document.body.removeChild(link);
				})
			}
		},
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
</style>
